<?php

namespace app\controller;

use support\Request;

class WmjBaseController
{
    protected $noNeedLogin = ['index','setAccesstoken','register','deldev'];
    public function index(Request $request)
    {
        return response(__CLASS__);
    }
    //发送json请求函数
    public function PostJson($url, $data = NULL,$headers="")
    {
        $curl = curl_init();
        $header =array(
            'Content-Type: application/json; charset=utf-8',
            'Content-Length:' . strlen(json_encode($data)),
            'Cache-Control: no-cache',
            'Pragma: no-cache'
        );
        if($headers){
            $header[]=$headers;
        }
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
        if (!$data) {
            return 'data is null';
        }
        if (is_array($data)) {
            $data = json_encode($data);
        }
        curl_setopt($curl, CURLOPT_POST, 1);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
        curl_setopt($curl, CURLOPT_TIMEOUT, 60); // 设置超时限制防止死循环
        curl_setopt($curl, CURLOPT_HEADER, 0);
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
        $res = curl_exec($curl);
        $errorno = curl_errno($curl);
        if ($errorno) {
            return $errorno;
        }
        curl_close($curl);
        return json_decode($res,true);
    }
    //组装发送请求，带上appid，appsecret
    public function Request($path, $data = [],$app_id='',$app_secret='')
    {
        $data["app_id"] = $app_id;
        $data["app_secret"] = $app_secret;
        $res = $this->PostJson("https://wdev.wmj.com.cn/deviceApi/".$path, $data);
        return $res;
    }
    //注册绑定
    public function register(Request $request)
    {
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $res = $this->Request("register", ["device_sn" => $device_sn],$app_id,$app_secret);
        return json($res);
    }
    //删除绑定
    public function deldev(Request $request)
    {
        $app_id = $request->post('app_id','');
        if(!$app_id){
            return json(['code'=>0,'msg'=>'缺失参数app_id']);
        }
        $app_secret = $request->post('app_secret','');
        if(!$app_secret){
            return json(['code'=>0,'msg'=>'缺失参数app_secret']);
        }
        $device_sn = $request->post('device_sn','');
        if(!$device_sn){
            return json(['code'=>0,'msg'=>'缺失参数device_sn']);
        }
        $res = $this->Request("logout", ["device_sn" => $device_sn],$app_id,$app_secret);
        return json($res);
    }
}
